package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.GoodsClass;
import com.api.shopapi.entity.GoodsClassAttributeRel;
import com.api.shopapi.entity.dto.GoodsClassDTO;
import com.api.shopapi.mapper.GoodsClassAttributeRelMapper;
import com.api.shopapi.mapper.GoodsClassMapper;
import com.api.shopapi.service.GoodsClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("GoodsClassServiceImplOne")
public class GoodsClassServiceImpl implements GoodsClassService {

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private GoodsClassAttributeRelMapper goodsClassAttributeRelMapper;

    @Override
    @Transactional
    public int insert(GoodsClass goodsClass, JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.size(); i++) {
            String goodsAttributeCode = jsonArray.get(i).toString();
            GoodsClassAttributeRel goodsClassAttributeRel=new GoodsClassAttributeRel();
            goodsClassAttributeRel.setGoodsClassAttributeRelCode(UUID.randomUUID().toString());
            goodsClassAttributeRel.setSort(i+1);
            goodsClassAttributeRel.setGoodsClassCode(goodsClass.getGoodsClassCode());
            goodsClassAttributeRel.setGoodsAttributeCode(goodsAttributeCode);
            goodsClassAttributeRelMapper.insert(goodsClassAttributeRel);
        }
        return goodsClassMapper.insert(goodsClass);
    }

    @Override
    public int delete(String goodsClassCode) {
        return goodsClassMapper.delete(goodsClassCode);
    }

    @Override
    @Transactional
    public int update(GoodsClass goodsClass,JSONArray jsonArray) {

        List<GoodsClassAttributeRel> goodsClassAttributeRelList = goodsClassAttributeRelMapper.getByGoodsClassCode(goodsClass.getGoodsClassCode());
        if (goodsClassAttributeRelList!=null&&goodsClassAttributeRelList.size()>0){
            for (GoodsClassAttributeRel goodsClassAttributeRel : goodsClassAttributeRelList) {//删除原来分类属性关系
                goodsClassAttributeRelMapper.delete(goodsClassAttributeRel.getGoodsClassAttributeRelCode());
            }
        }
        for (int i = 0; i < jsonArray.size(); i++) {//添加新的分类属性关系
            String goodsAttributeCode = jsonArray.get(i).toString();
            GoodsClassAttributeRel goodsClassAttributeRel=new GoodsClassAttributeRel();
            goodsClassAttributeRel.setGoodsClassAttributeRelCode(UUID.randomUUID().toString());
            goodsClassAttributeRel.setSort(i+1);
            goodsClassAttributeRel.setGoodsClassCode(goodsClass.getGoodsClassCode());
            goodsClassAttributeRel.setGoodsAttributeCode(goodsAttributeCode);
            goodsClassAttributeRelMapper.insert(goodsClassAttributeRel);
        }
        return goodsClassMapper.update(goodsClass);
    }

    @Override
    public GoodsClass getByGoodsClassCode(String goodsClassCode) {
        return goodsClassMapper.getByGoodsClassCode(goodsClassCode);
    }

    @Override
    public GoodsClass getByName(String name) {
        return goodsClassMapper.getByName(name);
    }

    @Override
    public List<GoodsClass> getAllList() {
        return goodsClassMapper.getAllList();
    }

    @Override
    public PageInfo<GoodsClass> getPageList(int pageIndex, int pageSize, String name,int status) {
        PageHelper.startPage(pageIndex,pageSize);
        List<GoodsClass> goodsClassList = goodsClassMapper.getPageList(name,status);
        PageInfo<GoodsClass> goodsClassPageInfo=new PageInfo<>(goodsClassList);
        return goodsClassPageInfo;
    }

    @Override
    public List<GoodsClassDTO> getListByParentGoodsClassCode(String parentGoodsClassCode) {
        return goodsClassMapper.getListByParentGoodsClassCode(parentGoodsClassCode);
    }

    @Override
    public List<GoodsClassDTO> getAllListByParentGoodsClassCode(String parentGoodsClassCode) {
        return goodsClassMapper.getAllListByParentGoodsClassCode(parentGoodsClassCode);
    }

    @Override
    public PageInfo<GoodsClassDTO> getPageListByParentGoodsClassCode(int pageIndex, int pageSize, String name,String parentGoodsClassCode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<GoodsClassDTO> goodsClassList = goodsClassMapper.getPageListByParentGoodsClassCode(parentGoodsClassCode, name);
        return new PageInfo<>(goodsClassList);
    }
}
